<?
session_start();
include ('../variables.php');
include($baseDIR.'/login/variables.php');
include_once($baseDIR.'/init.php');
//$webDIR="login";
//$save_type="MySQL";

$dbh = get_master_connect();
if (!$dbh) {
 	echo "We are sorry but we cannot process your request at the moment<BR>";
 	exit_now();
}

// Decode query string
if($_SERVER['QUERY_STRING']){
  $params = decode_params();
  if($params) {
    while(list($key,$value) = each($params)){
    	$$key  = $value;
    }
  }
  $params = '';
}

if (getenv(REQUEST_METHOD) == "POST" || ($username && $password)) {

	// Patched version for World pay order type form
	$userName =  strtolower(validate( (($HTTP_POST_VARS['username']) ? $HTTP_POST_VARS[username] : $username ) ));
	$passWord = validate((($HTTP_POST_VARS['password']) ? $HTTP_POST_VARS['password'] : $password));
	
	if (!$userName || !$passWord) header("Location: $baseURL/$webDIR/login.html");
	
//	kill_old_sessions();
//  $sid = session_get_unique_id();

	$errorMsg = "";
	$okMsg = "";
	$statusMsg = "";
	$id = get_reseller_id($userName);
	$resellerCountry = get_reseller_country($id);

	$_SESSION['memberAuthenticated']= "yes";
	$_SESSION['userName'] = $userName;
	$_SESSION['id'] = $id;

	$sid = session_id();
//	session_register_(array(errorMsg, okMsg, statusMsg, memberAuthenticated, userName, passWord, id, resellerCountry), sid);
//	session_save(sid);
	$params['sid'] = $sid;
	// Patch for WorldPay
	if (!empty($_POST['manual_order'])) {
		$params['action'] = 'order_wp';
	}
	else {
		$params['action'] = $action;
	}

	if($action == 'manageDomains'){
		$params['start_ajax'][] = "loadPlan('$_POST[dns]');";
	}

	// Patch for reseller login when buying a hosting plan
   if (!empty($_POST['plan'])) {
   	$params['plan'] = $_POST['plan'];
   }
   else {
   	$params['plan'] = $plan;
   }

	if (!check_user_pass($userName, $passWord)) {
		header('Location: '.$baseURL.'/'.$webDIR.'/login.html?'.encode_params($params).'&errorMsg=wrong password');
	}
	else {
		$url="$baseURL/$webDIR/?".encode_params($params);
		header("Location: $url");
	}
}

?>
